package forBFK;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import net.sf.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.honeymoondesert.analyzer.AddressAnalyzer;
import com.honeymoondesert.util.JsonUtil;
import com.honeymoondesert.util.PropertyHelper;
import com.sun.xml.internal.ws.message.RelatesToHeader;

public class RelationCSV {
	public static String jsonToCSV(JsonObject jsonObject){
		String owrite="";
		owrite=jsonObject.get("storeName")+","
				+jsonObject.get("district")+","
				+jsonObject.get("address")+","
				+jsonObject.get("tel")+","
				+jsonObject.get("commentNum")+","
				+jsonObject.get("score")+","
				+jsonObject.get("advPrice")+","
				+jsonObject.get("taste")+","
				+jsonObject.get("enviroment")+","
				+jsonObject.get("service")+","
				+jsonObject.get("date")+","
				+jsonObject.get("storeUrl")
				+"\n\r";
		return owrite;
	}
	public static void main(String[] args) throws IOException{
		PropertyHelper ph=new PropertyHelper(null);
		String output=ph.getProperties("output");
		File file=new File(output+"baseStore");
		FileOutputStream fos;
		fos = new FileOutputStream(new File(output+"relation"+".csv"));
		//csv抬头
		String owrite="附近满记,类型,店名,区,地址,电话,评论数,评分,均价,口味,环境,服务,日期,点评链接\n\r";
		fos.write(owrite.getBytes());
		for (File baseFile : file.listFiles()) {
			JsonUtil ju=new JsonUtil();
			JsonObject baseJSON=new Gson().fromJson(ju.ReadFile(baseFile.getAbsolutePath()),JsonObject.class);
			JsonObject roadinfo=AddressAnalyzer.analyze(baseJSON.get("address").getAsString());
			baseJSON.addProperty("road", roadinfo.get("road")==null?"":roadinfo.get("road").getAsString());
			baseJSON.addProperty("number", roadinfo.get("number")==null?"":roadinfo.get("number").getAsString());
			baseJSON.addProperty("floor", roadinfo.get("floor")==null?"":roadinfo.get("floor").getAsString());
			
			List<JsonObject> same = AddressAnalyzer.getSameBuilding(baseJSON);
			List<JsonObject> surround = AddressAnalyzer.getSurroundBuilding(baseJSON);
			List<JsonObject> surroundbyBaidu=AddressAnalyzer.getSurroundBuildingByBaidu(baseJSON, 1000);
			
			try {
				//满记店信息
				owrite="\"无\",\"满记店\","+RelationCSV.jsonToCSV(baseJSON);
				fos.write(owrite.getBytes());
				//同一栋楼商铺信息
				for (JsonObject jsonObject : same) {
					owrite=baseJSON.get("storeName")+",\"同一栋楼\","+RelationCSV.jsonToCSV(jsonObject);
					fos.write(owrite.getBytes());
				}
				//周边商铺信息
				for (JsonObject jsonObject : surround) {
					owrite=baseJSON.get("storeName")+",\"附近楼层\","+RelationCSV.jsonToCSV(jsonObject);
					fos.write(owrite.getBytes());
				}
				//百度接口的周边1000m之内的店家信息
				for (JsonObject jsonObject : surround) {
					owrite=baseJSON.get("storeName")+","+jsonObject.get("distance").getAsString()+"米,"+RelationCSV.jsonToCSV(jsonObject);
					fos.write(owrite.getBytes());
				}
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		fos.close();
		
	}
}
